package com.hxx.day8;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 序列化
 */
public class TestDemo2 {

    /**
     * 序列化
     */
    @Test
    public void test1()throws Exception{
        ObjectOutputStream outputStream =
                new ObjectOutputStream(new FileOutputStream("d:/zmx/aa.txt"));
        //对象序列化
        Student student = new Student(1001,"张三",11);
        student.setPassword("123456");
        outputStream.writeObject(student);
        outputStream.flush();
        outputStream.close();
    }
    /**
     * 反序列化
     */
    @Test
    public void test2()throws Exception{
        ObjectInputStream inputStream =
                new ObjectInputStream(new FileInputStream("d:/zmx/aa.txt"));
        //对象反序列化
        Student student = (Student) inputStream.readObject();
        System.out.println(student);
        inputStream.close();
    }

    /**
     * 序列化和反序列化的注意事项
     * 1.序列化时，需要实现Serializable接口，否则会抛出NotSerializableException异常
     * 2.序列化时，建议添加serialVersionUID，否则会抛出InvalidClassException异常
     * 添加serialVersionUID的目的就是为了兼容性
     */
    @Test
    public void test3()throws Exception{
        ObjectInputStream inputStream =
                new ObjectInputStream(new FileInputStream("d:/zmx/aa.txt"));
        //对象反序列化
        Student student = (Student) inputStream.readObject();
        System.out.println(student);
        inputStream.close();
    }
    @Test
    public void test4()throws Exception{

    }
}

class Student implements Serializable {

   //private static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private int age;
    //transient关键字可以修饰成员变量，用于表示该成员变量不参与序列化和反序列化
    private transient String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Student() {
    }

    public Student(Integer id, String name, int age, String password) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.password = password;
    }

    public Student(String name, Integer id) {
        this.id = id;
        this.name = name;
    }

    public Student(Integer id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", password='" + password + '\'' +
                '}';
    }
}


























